package controllers

import (
	"fmt"
	"gonexample/app/models"
	"gonexample/gone/e"
	"gonexample/gone/glog"
	"net/http"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
)

func Ping(c *gin.Context) {
	ip := c.ClientIP()

	body, _ := c.GetRawData()

	glog.SetLogger("ping")
	glog.Debug("GET %s POST %s IP %s", c.Request.RequestURI, string(body), ip)

	a := c.Request.URL.Path
	glog.Info(a)

	session := sessions.Default(c)

	// session.Set("Hello", "World")
	// session.Save()

	c.XML(http.StatusOK, e.New(e.OK).SetData(session.Get("Hello")))
	return
}

func User(c *gin.Context) {
	// ip := c.ClientIP()

	// glog.SetLogger("user")
	// glog.Debug("GET %s IP %s", c.Request.RequestURI, ip)

	session := sessions.Default(c)

	id := session.Get("userId")

	user, err := models.GetUser(id)
	fmt.Println(user, err)
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			c.XML(http.StatusOK, e.New(e.ErrNotFound))
			return
		}
		c.XML(http.StatusOK, e.New(e.ErrNotFound))
		return
	}

	c.XML(http.StatusOK, e.New(e.OK).SetData(user))
	return
}
